package com.xquant.databasemanagement.controller.windows;

import com.xquant.databasemanagement.bean.table.ColumnBean;
import com.xquant.databasemanagement.bean.table.PrimaryKeyBean;
import com.xquant.databasemanagement.controller.FormController;
import com.xquant.databasemanagement.enums.KeyType;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.apache.commons.lang.StringUtils;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

public class PrimaryKeyWindowController implements Initializable, FormController {


    public TextField tablespace;
    public ComboBox<KeyType> keyType;

    @FXML
    private TableView<Field> columnTable;

    @FXML
    private VBox primaryKeyWindow;

    @FXML
    private ComboBox<String> primaryKeyColumn;

    @FXML
    private TextField primaryKeyName;

    private ObservableList<ColumnBean> columnNames;

    private PrimaryKeyBean primaryKeyBean = null;




    @Override
    public void initialize(URL location, ResourceBundle resources) {
        keyType.getItems().addAll(KeyType.values());
    }

    @FXML
    public void eventSavePrincipalKeyButtonClick(ActionEvent event) {
        if (isValid()) {
            Stage parentWin = (Stage) primaryKeyWindow.getScene().getWindow();
            if (primaryKeyBean == null) {
                setPrimaryKeyBean();
                TableView view = ((TableView) parentWin.getOwner().getScene().lookup("#primaryKeyTable"));
                view.getItems().addAll(this.primaryKeyBean);
            }else
                setPrimaryKeyBean();
            clear();
            parentWin.close();
        }
    }



    public void initColumnNames(ObservableList<ColumnBean> columnNames){
        primaryKeyColumn.getItems().clear();
        this.columnNames = columnNames;
        for (ColumnBean p:this.columnNames) {
            primaryKeyColumn.getItems().addAll(p.getColumnName());
        }
    }


    public void setPrimaryKeyBean(PrimaryKeyBean primaryKeyBean){
        this.primaryKeyBean = primaryKeyBean;
        primaryKeyName.setText(primaryKeyBean.getPrimaryKeyName());
        String columns[] = primaryKeyBean.getPrimaryKeyColumn().split(",");
        for (String column : columns) {
            columnTable.getItems().addAll(new Field(column));
        }
        tablespace.setText(primaryKeyBean.getTablespace());
        if (primaryKeyBean.getKeyType()!=null&&!primaryKeyBean.getKeyType().equals(""))
            keyType.getSelectionModel().select(KeyType.valueOf(primaryKeyBean.getKeyType()));
    }


    private void setPrimaryKeyBean(){
        if (primaryKeyBean==null)
            primaryKeyBean = new PrimaryKeyBean();
        primaryKeyBean.setPrimaryKeyName(primaryKeyName.getText());

        primaryKeyBean.setTablespace(tablespace.getText());
        primaryKeyBean.setKeyType(keyType.getValue().toString());
        List<Field> fields = columnTable.getItems();
        primaryKeyBean.setPrimaryKeyColumn(fields.get(0).getFieldName());
        for (int i = 1;i<fields.size();i++) {
            primaryKeyBean.setPrimaryKeyColumn(primaryKeyBean.getPrimaryKeyColumn()+","+fields.get(i).getFieldName());
        }
    }


    @Override
    public void clear(){
        primaryKeyBean = null;
        primaryKeyName.clear();
        primaryKeyColumn.getSelectionModel().select(-1);
        tablespace.clear();
        columnTable.getItems().clear();
    }

    @Override
    public boolean isValid() {

        boolean matches = primaryKeyName.getText().matches("^\\w+$");
        if (!matches){
            new Alert(Alert.AlertType.NONE, "主键命名正确", new ButtonType[]{ButtonType.CLOSE}).show();
            return false;
        }else if (columnTable.getItems()==null||columnTable.getItems().size()<=0){
            new Alert(Alert.AlertType.NONE, "请选择主键字段", new ButtonType[]{ButtonType.CLOSE}).show();
            return false;
        }else if(keyType.getSelectionModel().getSelectedItem()==null){
            new Alert(Alert.AlertType.NONE, "请选择候选键类型", new ButtonType[]{ButtonType.CLOSE}).show();
            return false;
        } else
            return true;
    }

    @FXML
    private void eventAddColumnButtonClick(ActionEvent event) {

        if (primaryKeyColumn.getSelectionModel().getSelectedIndex()!=-1&&
                !columnTable.getItems().contains(new Field(primaryKeyColumn.getSelectionModel().getSelectedItem()))) {
            columnTable.getItems().addAll(new Field(primaryKeyColumn.getSelectionModel().getSelectedItem()));
        }
    }

    @FXML
    private void eventDeleteColumnButtonClick(ActionEvent event) {
        if (columnTable.getSelectionModel().getSelectedIndex()!=-1){
            columnTable.getItems().remove(columnTable.getSelectionModel().getSelectedItem());
        }
    }

    public class Field{
        private final SimpleStringProperty fieldName = new SimpleStringProperty();


        public Field(String fieldName){
            this.fieldName.set(fieldName);
        }

        public String getFieldName() {
            return fieldName.get();
        }

        public SimpleStringProperty fieldNameProperty() {
            return fieldName;
        }

        public void setFieldName(String fieldName) {
            this.fieldName.set(fieldName);
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (obj instanceof Field){
                Field field = (Field) obj;
                return StringUtils.equals(field.getFieldName(),this.getFieldName());
            }
            return false;

        }
    }
}
